package com.zmj.sy.mom.srv.aps.utils;

import cn.hutool.core.date.BetweenFormatter.Level;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RuntimeUtil;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.lang.management.ManagementFactory;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具类
 *
 * @author pxd
 */
public class DateUtils extends DateUtil {
    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};





    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static final String datePath() {
        return  DateUtil.format(new Date(), "yyyy/MM/dd");
    }

    /**
     * 日期路径 即时分秒 如122010
     */
    public static final String dateTime() {
        Date now = new Date();
        return DateFormatUtils.format(now, "HHmmSS");
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        }
         return parse(str.toString(), parsePatterns); 
      
    }
    public static final String getTime() {
        return DateUtil.now();
    }
    /**
     * 计算两个时间的分钟差 endTime- beginTime
     *
     * @param beginTime 时间
     * @param endTime
     * @return
     */
    public static long getSubMinutes(Date beginTime, Date endTime) {
        //从对象中拿到时间
        return between(beginTime, endTime, DateUnit.MINUTE);
    }
    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate() {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 计算两个时间差
     */
    public static String getDatePoor(Date endDate, Date nowDate) {
        return  DateUtil.formatBetween(between(nowDate, endDate, DateUnit.MINUTE), Level.SECOND);
    }


    public static void main(String[] args) {
    /*	System.out.println(RuntimeUtil.execForStr("ipconfig"));
    	System.out.println(RuntimeUtil.getProcessorCount());*/
        System.out.println(DateUtils.format(new Date(), "yyyy-MM-dd"));
    }

    public static boolean isEffectiveDate2(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

}
